/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.draw;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.util.ListIterator;

import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.geo.GCubicCurve;
import br.ufmg.dees.insane.model.geo.GCurve;
import br.ufmg.dees.insane.model.geo.GLine;
import br.ufmg.dees.insane.model.geo.GPoint;
import br.ufmg.dees.insane.model.geo.GQuadCurve;
import br.ufmg.dees.insane.model.geo.GRegion;

public class LoftingRegionDraw extends RegionDraw {

//*****************************************************************************
	
	public LoftingRegionDraw(DrawingArea da, Object represents) {
		super(da, represents);
	}
    
//*****************************************************************************
	
    /**
     * Draws in a given Graphics.
     *
     * @see   java.awt.Graphics
     */
	public void draw(Graphics2D g2) {
		GeneralPath region = new GeneralPath();
		Point p1, p2, p3;
		GRegion rg = (GRegion)getRepresents();
		ListIterator curves = rg.getCurves().listIterator();
		
		GCurve gcv = (GCurve)curves.next();
		p1 = da.getViewPoint(gcv.getFirstPoint());
		float x4 = (float)p1.getX();
		float y4 = (float)p1.getY();
		region.moveTo(x4,y4);
		int x = (int)x4;
		int y = (int)y4;
		int np = 1;
		ListIterator primitives = gcv.getPrimitives().listIterator();
		while(primitives.hasNext()) {
			Object obj = primitives.next();
			if (obj instanceof GPoint) {
				GPoint prt = (GPoint)obj;
				p1 = da.getViewPoint(prt.getP2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
				x = x + (int)x1;
				y = y + (int)y1;
				np++;
			}
			if (obj instanceof GLine) {
				Line2D prt = (Line2D)obj;
				p1 = da.getViewPoint(prt.getP2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
				x = x + (int)x1;
				y = y + (int)y1;
				np++;
			}
			if (obj instanceof GQuadCurve) {
				GQuadCurve prt = (GQuadCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getP2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				region.quadTo(x1,y1,x2,y2);
				x = x + (int)x2;
				y = y + (int)y2;
				np++;
			}
			if (obj instanceof GCubicCurve) {
				GCubicCurve prt = (GCubicCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getCtrl2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				p3 = da.getViewPoint(prt.getP2());
				float x3 = (float)p3.getX();
				float y3 = (float)p3.getY();
				region.curveTo(x1,y1,x2,y2,x3,y3);
				x = x + (int)x3;
				y = y + (int)y3;
				np++;
			}
       	}
		
		gcv = (GCurve)curves.next();
		p1 = da.getViewPoint(gcv.getEndPoint());
		x4 = (float)p1.getX();
		y4 = (float)p1.getY();
		region.lineTo(x4,y4);
		x = x + (int)x4;
		y = y + (int)y4;
		np++;
		primitives = gcv.getPrimitives().listIterator( gcv.getPrimitives().size() );
		while(primitives.hasPrevious()) {
			Object obj = primitives.previous();
			if (obj instanceof GPoint) {
				GPoint prt = (GPoint)obj;
				p1 = da.getViewPoint(prt.getP1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
				x = x + (int)x1;
				y = y + (int)y1;
				np++;
			}
			if (obj instanceof GLine) {
				Line2D prt = (Line2D)obj;
				p1 = da.getViewPoint(prt.getP1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
				x = x + (int)x1;
				y = y + (int)y1;
				np++;
			}
			if (obj instanceof GQuadCurve) {
				GQuadCurve prt = (GQuadCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getP1());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				region.quadTo(x1,y1,x2,y2);
				x = x + (int)x2;
				y = y + (int)y2;
				np++;
			}
			if (obj instanceof GCubicCurve) {
				GCubicCurve prt = (GCubicCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getCtrl1());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				p3 = da.getViewPoint(prt.getP1());
				float x3 = (float)p3.getX();
				float y3 = (float)p3.getY();
				region.curveTo(x1,y1,x2,y2,x3,y3);
				x = x + (int)x3;
				y = y + (int)y3;
				np++;
			}
       	}
		region.closePath();
		g2.fill(region);
		mediumX = (int)(x/np) - 5;
		mediumY = (int)(y/np) + 5;
		
		Color currentColor = g2.getColor();
		if (isSelected()) {
			g2.setColor( Color.red );
			g2.fill(region);
			if ( rg.getElements().isEmpty() ) {
				g2.setColor( Color.black );
				this.drawLabel(g2);
			}
		}
		g2.setColor(currentColor);
	}
	
//*****************************************************************************
	
	public void drawBoundary(Graphics2D g2) {
		Point p1, p2, p3;
		GRegion rg = (GRegion)getRepresents();
		ListIterator curves = rg.getCurves().listIterator();
		while(curves.hasNext()) {
			GCurve gcv = (GCurve)curves.next();
			GeneralPath curve = new GeneralPath();
			p1 = da.getViewPoint(gcv.getFirstPoint());
			float x4 = (float)p1.getX();
			float y4 = (float)p1.getY();
			curve.moveTo(x4,y4);
			ListIterator primitives = gcv.getPrimitives().listIterator();
			while(primitives.hasNext()) {
				Object obj = primitives.next();
				if (obj instanceof GPoint) {
					GPoint prt = (GPoint)obj;
					p1 = da.getViewPoint(prt.getP2());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					curve.lineTo(x1,y1);
				}
					if (obj instanceof GLine) {
					Line2D prt = (Line2D)obj;
					p1 = da.getViewPoint(prt.getP2());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					curve.lineTo(x1,y1);
				}
					if (obj instanceof GQuadCurve) {
					GQuadCurve prt = (GQuadCurve)obj;
					p1 = da.getViewPoint(prt.getCtrl());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					p2 = da.getViewPoint(prt.getP2());
					float x2 = (float)p2.getX();
					float y2 = (float)p2.getY();
					curve.quadTo(x1,y1,x2,y2);
				}
				if (obj instanceof GCubicCurve) {
					GCubicCurve prt = (GCubicCurve)obj;
					p1 = da.getViewPoint(prt.getCtrl1());
					float x1 = (float)p1.getX();
					float y1 = (float)p1.getY();
					p2 = da.getViewPoint(prt.getCtrl2());
					float x2 = (float)p2.getX();
					float y2 = (float)p2.getY();
					p3 = da.getViewPoint(prt.getP2());
					float x3 = (float)p3.getX();
					float y3 = (float)p3.getY();
					curve.curveTo(x1,y1,x2,y2,x3,y3);
				}
       		}
       		g2.draw(curve);
		}
	}
	
//*****************************************************************************
	
	/**
	 * Returns the draw bounds in device units.
	 *
	 * @return the draw bounds
	 */
	public GeneralPath getRegion() {
		GeneralPath region = new GeneralPath();
		Point p1, p2, p3;
		GRegion rg = (GRegion)getRepresents();
		ListIterator curves = rg.getCurves().listIterator();
		
		GCurve gcv = (GCurve)curves.next();
		p1 = da.getViewPoint(gcv.getFirstPoint());
		float x4 = (float)p1.getX();
		float y4 = (float)p1.getY();
		region.moveTo(x4,y4);
		ListIterator primitives = gcv.getPrimitives().listIterator();
		while(primitives.hasNext()) {
			Object obj = primitives.next();
			if (obj instanceof GPoint) {
				GPoint prt = (GPoint)obj;
				p1 = da.getViewPoint(prt.getP2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
			}
			if (obj instanceof GLine) {
				Line2D prt = (Line2D)obj;
				p1 = da.getViewPoint(prt.getP2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
			}
			if (obj instanceof GQuadCurve) {
				GQuadCurve prt = (GQuadCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getP2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				region.quadTo(x1,y1,x2,y2);
			}
			if (obj instanceof GCubicCurve) {
				GCubicCurve prt = (GCubicCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getCtrl2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				p3 = da.getViewPoint(prt.getP2());
				float x3 = (float)p3.getX();
				float y3 = (float)p3.getY();
				region.curveTo(x1,y1,x2,y2,x3,y3);
			}
       	}
		
		gcv = (GCurve)curves.next();
		p1 = da.getViewPoint(gcv.getEndPoint());
		x4 = (float)p1.getX();
		y4 = (float)p1.getY();
		region.lineTo(x4,y4);
		primitives = gcv.getPrimitives().listIterator( gcv.getPrimitives().size() );
		while(primitives.hasPrevious()) {
			Object obj = primitives.previous();
			if (obj instanceof GPoint) {
				GPoint prt = (GPoint)obj;
				p1 = da.getViewPoint(prt.getP1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
			}
			if (obj instanceof GLine) {
				Line2D prt = (Line2D)obj;
				p1 = da.getViewPoint(prt.getP1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				region.lineTo(x1,y1);
			}
			if (obj instanceof GQuadCurve) {
				GQuadCurve prt = (GQuadCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getP1());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				region.quadTo(x1,y1,x2,y2);
			}
			if (obj instanceof GCubicCurve) {
				GCubicCurve prt = (GCubicCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getCtrl1());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				p3 = da.getViewPoint(prt.getP1());
				float x3 = (float)p3.getX();
				float y3 = (float)p3.getY();
				region.curveTo(x1,y1,x2,y2,x3,y3);
			}
       	}
		
		region.closePath();
		return region;
	}
	
//*****************************************************************************
}
